package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.data.core.db.ColumnInformation;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MemCachedAdapter extends TableDataAdapter
{
  private static final long serialVersionUID = 1L;
  private List row_list;

  public MemCachedAdapter(DatabaseConnection paramDatabaseConnection, String paramString, int paramInt)
  {
    super(paramDatabaseConnection, paramString, paramInt);
  }

  public int getRowCount()
    throws TableDataException
  {
    hasRow(2147483647);
    return this.row_list.size();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return ((hasRow(paramInt1)) ? ((Object[])this.row_list.get(paramInt1))[paramInt2] : null);
  }

  protected synchronized boolean hasRow(int paramInt)
    throws TableDataException
  {
    if ((this.row_list != null) && (this.row_list.size() > paramInt))
      return true;
    if (this.isCheckRowOver)
      return false;
    initConnectionAndResultAndCheckInColumns();
    if (this.connection == null)
      return false;
    Dialect localDialect = DialectFactory.generateDialect(this.connection);
    int i = getColumnCount();
    String str1 = this.database.getOriginalCharsetName();
    String str2 = this.database.getNewCharsetName();
    try
    {
      do
      {
        if (!(this.resultSet.next()))
          break label181;
        Object[] arrayOfObject = new Object[i];
        for (int j = 0; j < i; ++j)
          try
          {
            arrayOfObject[j] = DataUtils.getResultSetObject(this.resultSet, this.columnInformations[j].getColumnType(), j + 1, str1, str2, localDialect);
          }
          catch (SQLException localSQLException)
          {
            arrayOfObject[j] = null;
          }
        this.row_list.add(arrayOfObject);
      }
      while (this.row_list.size() <= paramInt);
      return true;
      label181: this.isCheckRowOver = true;
      releaseConnection();
    }
    catch (Exception localException1)
    {
      try
      {
        releaseConnection();
      }
      catch (Exception localException2)
      {
        FRContext.getLogger().log(Level.WARNING, "Error happens while releaseConnection");
      }
      if (localException1.getCause() != null)
        FRContext.getLogger().log(Level.WARNING, "Caused By:" + localException1.getCause().getMessage(), localException1.getCause());
      this.isCheckRowOver = true;
      throw new TableDataException(localException1.getMessage() + "\nQuery is:" + this.sql + "\nrowIndex to get is:" + paramInt, localException1);
    }
    return (this.row_list.size() > paramInt);
  }

  protected void initRowValueStatus()
  {
    this.row_list = new ArrayList();
  }

  protected synchronized void release()
    throws SQLException
  {
    if (this.row_list != null)
      this.row_list.clear();
    this.isCheckRowOver = false;
    super.release();
  }
}